package com.yy.community.modules.article.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.community.common.base.Result;
import com.yy.community.common.utils.PageUtils;
import com.yy.community.common.utils.Query;
import com.yy.community.common.validator.ValidatorUtils;
import com.yy.community.common.validator.group.AddGroup;
import com.yy.community.modules.article.entity.Article;
import com.yy.community.modules.article.service.IArticleService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章类控制器
 * </p>
 *
 * @author 司马缸砸缸了
 * @since 2019-07-12
 */

@Api(value = "文章类控制器", tags = "文章")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @ApiOperation(value = "分页查询文章接口", notes = "条件，分页查询")
    @GetMapping("")
    public Result getPage(@ApiParam(value = "请求过滤参数") @RequestParam Map<String, Object> params) {
        log.info("分页查询所有文章参数:{}", JSONUtil.toJsonStr(params));
        PageUtils page = articleService.queryPage(params);

        return Result.ok(page);
    }

    @ApiOperation(value = "条件查询文章接口", notes = "条件，不分页查询")
    @GetMapping("/list")
    public Result getList(Article article) {
        log.info("条件查询文章参数:{}", JSONUtil.toJsonStr(article));
        List<Article> list = articleService.queryList(article);

        return Result.ok(list);
    }

    @ApiOperation(value = "ID查询文章", notes = "根据ID查询文章")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键Id", required = true, dataType = "string"),})
    @GetMapping("/{id}")
    public Result getArticle(@PathVariable String id) {
        log.info("ID查询文章参数:{}", id);
        Article article = articleService.queryById(id);

        return Result.ok(article);
    }

    @ApiOperation(value = "添加文章", notes = "添加文章")
    @PostMapping("/")
    public Result save(@ApiParam(value = "Article实体类", required = true) @RequestBody Article article) {
        log.info("添加文章参数:{}", JSONUtil.toJsonStr(article));
        ValidatorUtils.validateEntity(article, AddGroup.class);
        articleService.save(article);

        return Result.ok();
    }

    @ApiOperation(value = "更新文章", notes = "更新文章")
    @PutMapping("/")
    public Result update(@RequestBody Article article) {
        log.info("更新文章参数:{}", JSONUtil.toJsonStr(article));
        articleService.updateArticleById(article);

        return Result.ok();
    }

    @ApiOperation(value = "删除文章", notes = "删除文章")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        log.info("删除文章参数:{}", id);
        articleService.deleteArticleById(id);

        return Result.ok();
    }

}
